package common.fengkai.filepost.controller;

import common.fengkai.filepost.VO.ResVo;
import common.fengkai.filepost.bean.mysqlBean.EmpUser;
import common.fengkai.filepost.bean.pgbean.WatermarkSealInfo;
import common.fengkai.filepost.dao.mysqldao.EmpUserMapper;
import common.fengkai.filepost.dao.pgsql.WatermarkSealInfoMapper;
import common.fengkai.filepost.service.AsyncTask;
import common.fengkai.filepost.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app")
public class TestController {

    private static int count = 100;

    @Autowired
    private EmpUserMapper empUserMapper;

    @Autowired
    private WatermarkSealInfoMapper watermarkSealInfoMapper;

    @Autowired
    private AsyncTask asycnTaskImpl;

    @Autowired
    private TestService testService;

    @GetMapping("/hello")
    public String testhello(){
        return "hello world";
    }

    @GetMapping("/async")
    @Transactional(rollbackFor = Exception.class)
    public ResVo getAsyncTask () {
        try {
            asycnTaskImpl.getAsync1();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<EmpUser> empUsers = empUserMapper.selectAllByExample();
        EmpUser empUser = new EmpUser();
        Iterator<EmpUser> iterator = empUsers.iterator();
        while (iterator.hasNext()) {
            EmpUser next = iterator.next();
            log.debug(next.toString());
        }
        List<WatermarkSealInfo> watermarkSealInfos = watermarkSealInfoMapper.selectAll();
        Iterator<WatermarkSealInfo> watermarkSealInfoIterator = watermarkSealInfos.iterator();
        while (watermarkSealInfoIterator.hasNext()) {
            WatermarkSealInfo next = watermarkSealInfoIterator.next();
            log.debug(next.toString());
        }
        asycnTaskImpl.getAsync2();
        String su = "";
        su.replace(su, "ooo");
        return ResVo.ok("11111");
    }

    //41


    @PostMapping("/kill")
    public ResVo doKill () {
        Jedis jedis = new Jedis("120.48.31.109", 6379);
        Long lock = jedis.setnx("lock", "10000");
        if (lock == 1 && count > 0) {
            count--;
            jedis.del("lock");
            log.debug("库存数据---" + count);
            return ResVo.ok("success");
        } else {
            log.debug("锁占用-----秒杀失败");
            return ResVo.ok("failed");
        }
        // Long lock1 = jedis.del("lock");
    }

    @GetMapping("/user")
    public String user(){
        testService.selectByIds();
        return "hello world";
    }
}



